package com.power.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.ProdComm;
import com.power.model.Result;
import com.power.service.ProdCommService;
import com.power.vo.ProdCommData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 商品评论管理控制层
 * @Author dingchunlin
 * @Date 2024/07/15 18:10
 * @Version 1.0.0
 */
@Api(tags = "商品评论接口管理")
@RequestMapping("/prod/prodComm")
@RestController
public class ProdCommController {

    @Autowired
    private ProdCommService prodCommService;

    /**
     * 多条件分页查询商品评论
     * @param current 当前页
     * @param size 每页显示数量
     * @param prodName 商品名称
     * @param status 状态
     * @return Result<Page<ProdComm>>
     */
    @ApiOperation(value = "多条件分页查询商品评论")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String prodName,
                                                   @RequestParam(required = false) Integer status
                                                   ){
        // 构建分页对象
        Page<ProdComm> page = new Page<>(current, size);
        // 构建查询条件进行查询
        page = prodCommService.page(page,new LambdaQueryWrapper<ProdComm>()
               .like(StringUtils.hasText(prodName), ProdComm::getProdName, prodName)
               .eq(ObjectUtil.isNotEmpty(status), ProdComm::getStatus, status)
                .orderByDesc(ProdComm::getCreateTime));
        return Result.success(page);
    }

    /**
     * 根据标识查询评论
     * @param id 评论标识
     * @return Result<ProdComm>
     */
    @ApiOperation(value = "根据标识查询评论")
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@PathVariable("id") Long id){
        ProdComm prodComm = prodCommService.getById(id);
        return Result.success(prodComm);
    }

    /**
     * 回复和审核商品评论
     * @param prodComm 商品评论
     * @return Result<String>
     */
    @ApiOperation(value = "回复和审核商品评论")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('prod:prodComm:update')")
    public Result<String> replayAndExamineProdComm(@RequestBody ProdComm prodComm){
        Boolean flag = prodCommService.replayAndExamineProdComm(prodComm);
        return Result.handle(flag);
    }

    /**
     * 小程序查询商品评论总览信息
     * @param prodId 商品标识
     * @return Result<ProdCommData>
     */
    @ApiOperation(value = "小程序查询商品评论总览信息")
    @GetMapping("/prodComm/prodCommData")
    public Result<ProdCommData> loadWxProdCommData(@RequestParam Long prodId) {
        ProdCommData prodCommData = prodCommService.queryWxProdCommDataByProdId(prodId);
        return Result.success(prodCommData);
    }

    /**
     * 分页查询单个商品评论
     * @param prodId 商品标识
     * @param size 每页显示数量
     * @param current 当前页
     * @param evaluate 查询条件
     * @return Result<Page<ProdComm>>
     */
    @ApiOperation(value = "分页查询单个商品评论")
    @GetMapping("/prodComm/prodCommPageByProd")
    public Result<Page<ProdComm>> loadWxProdCommPageByProd(@RequestParam Long prodId,
                                                            @RequestParam Long size,
                                                            @RequestParam Long current,
                                                            @RequestParam Long evaluate) {
        // 根据商品分页查询商品评论
        Page<ProdComm> page = new Page<>(current, size);
        page = prodCommService.queryWxProdCommPageByProd(page, prodId, evaluate);
        return Result.success(page);
    }

}
